package com.evry.support;

import org.springframework.stereotype.Service;

@Service
public class FractionsImpl implements Fractions {

	@Override
	public int calculateNumerator(double decimalValue){
		return calculateFraction(decimalValue, true);
	}
	
	@Override
	public int calculateDenominator(double decimalValue){
		return calculateFraction(decimalValue, false);
	}
	
	private int calculateFraction(double decimalValue, boolean calculateNumerator) {

		double decimal = decimalValue;
		double originalDecimal;
		int LIMIT = 12;
		int denominators[] = new int[LIMIT + 1];
		int numerator = 0;
		int denominator = 0;
		int temp;
		int MAX_GOODNESS = 100;

		originalDecimal = decimal;

		// Display the header information
//		System.out.println("--------------------");
//		System.out.println("Input decimal number to be converted: " + decimal);
//		System.out.println();

		// Compute all the denominators
//		System.out.println("All computed denominators:");
		int i = 0;
		while (i < LIMIT + 1) {
			denominators[i] = (int) decimal;
//			System.out.print(denominators[i] + " ");
			decimal = 1.0 / (decimal - denominators[i]);
			i = i + 1;
		}
//		System.out.println();
//		System.out.println();

		// Compute the i-th approximation
		int last = 0;
		while (last < LIMIT) {

			// Print out the denominators used in this computation
//			System.out.print("Using these " + (last + 1) + " denominators: ");
//			for (int j = 0; j <= last; j++) {
//				System.out.print(denominators[j] + " ");
//			}
//			System.out.println();

			// Initialize variables used in computation
			numerator = 1;
			denominator = 1;
			temp = 0;

			// Do the computation
			int current = last;
			while (current >= 0) {
				denominator = numerator;
				numerator = (numerator * denominators[current]) + temp;
				temp = denominator;
				current = current - 1;
			}
			last = last + 1;

			// Display results
			double value = (double) numerator / denominator;
			int goodness = denominators[last];
			double error = 100 * Math.abs(value - originalDecimal)
					/ originalDecimal;

//			System.out.print("fraction = " + (int) numerator + "/"
//					+ (int) denominator);
//			System.out.print(", value = " + value);
//			System.out.print(", goodness = " + goodness);
//			System.out.println(", error = " + (int) error + "%");
//			System.out.println();

			// Exit early if we have reached our goodness criterion
			if (Math.abs(goodness) > MAX_GOODNESS)
				break;
		}
		if(calculateNumerator){
			return numerator;
		}
		else{
			return denominator;
		}
	}
}